/*
 * Copyright 2023 Huawei Cloud Computing Technology Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.huawei.cloudphone.virtualdevice.sensor;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Handler;
import android.os.HandlerThread;
import android.util.Log;

import com.huawei.cloudphone.virtualdevice.common.IVirtualDeviceDataListener;

public class VirtualSensor implements SensorEventListener {
    private static final String TAG = "VirtualSensor";
    private SensorManager mSensorManager;
    private Sensor mSensor;
    private Sensor mAccelerationSensor;
    private IVirtualDeviceDataListener mListener = null;
    private HandlerThread mHandlerThread;
    private boolean mIsStart = false;

    public VirtualSensor(SensorManager sensorManager) {
        mSensorManager = sensorManager;
        mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
        mAccelerationSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    }

    void registerSensorDataListener(IVirtualDeviceDataListener listener) {
        mListener = listener;
    }

    public void startProcess() {
        Log.i(TAG, "startProcess");
        if (mIsStart) {
            return;
        }
        mHandlerThread = new HandlerThread("sensorThread");
        mHandlerThread.start();
        Handler handler = new Handler(mHandlerThread.getLooper());
        mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_GAME, handler);
        mSensorManager.registerListener(this, mAccelerationSensor, SensorManager.SENSOR_DELAY_GAME, handler);
        mIsStart = true;
    }

    public void stopProcess() {
        Log.i(TAG, "stopProcess");
        if (!mIsStart) {
            return;
        }
        mSensorManager.unregisterListener(this, mSensor);
        mSensorManager.unregisterListener(this, mAccelerationSensor);
        try {
            mHandlerThread.join();
        } catch (InterruptedException e) {
            Log.e(TAG, "stopProcess: failed to stop mHandlerThread", e);
        }
        mIsStart = false;
    }

    @Override
    public void onSensorChanged(SensorEvent sensorEvent) {
        if (mListener == null) {
            return;
        }
        float x = sensorEvent.values[0];
        float y = sensorEvent.values[1];
        float z = sensorEvent.values[2];
        int type = sensorEvent.sensor.getType();
        if (type == Sensor.TYPE_ACCELEROMETER) {
            mListener.onRecvData(x, y, z, sensorEvent.accuracy, 0);
        } else if (type == Sensor.TYPE_GYROSCOPE) {
            mListener.onRecvData(x, y, z, sensorEvent.accuracy, 1);
        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int i) {
    }
}
